package com.doit.excersise.controller;


import java.time.LocalDateTime;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.doit.excersise.entity.TbUser;
import com.doit.excersise.service.ITbUserService;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author qingfeng
 * @since 2018-11-23
 */
@Controller
@RequestMapping("/user")
public class TbUserController {
	
	@Autowired
	private ITbUserService userService;
	
	@RequestMapping("checkUser")
	@ResponseBody
	public String checkUser(String userName) {
		QueryWrapper<TbUser> query = new QueryWrapper<>();
		query.eq("user_name", userName);
		List<TbUser> list = userService.list(query);
		if(list!=null&&list.size()>0) {
			return "该用户已存在";
		}
		return "成功";
	}
	
	
	@RequestMapping("register")
	public String register(TbUser user) {
		System.out.println(user);
		user.setUserCreateTime(LocalDateTime.now());
		user.setUserUpdateTime(LocalDateTime.now());
		user.setUserStatus(1);
		userService.save(user);
		return "select.html";
	}
	
	@RequestMapping("goPage")
	public String jump(HttpServletRequest request) {
		Object au = request.getSession().getAttribute("userName");
		if(au !=null) {
			return "select.html";
		}
		return "redirect:/index.html";
	}
	
	
	@RequestMapping("login")
	@ResponseBody
	public String login(TbUser user,HttpServletRequest request) {
		QueryWrapper<TbUser> query = new QueryWrapper<>();
		query.eq("user_Name", user.getUserName());
		query.eq("user_pwd",user.getUserPwd());
		
		List<TbUser> list = userService.list(query);
		if(list!=null && list.size()>0) {
			TbUser u = list.get(0);
			request.getSession().setAttribute("userName", u);
			return "success";
		}
		return "用户名或密码错误";
	}
	
	
	
	
}

